unit unitStatistic;

interface

uses Windows, SysUtils, Classes;

var
    allRecv: DWORD;
    allSent: DWORD;
    allConn: DWORD;
    allWkgTime: DWORD;

    allPacked: DWORD;

procedure RenewCharts;
procedure RenewStaticDatas;
function SizeDiv(Sz: DWORD; postB: BOOL): String;

implementation

uses Unit1;

function SizeDiv(Sz: DWORD; postB: BOOL): String;
var
  pre: Byte;
const
  cpre: array [1 .. 5] of char = ('K', 'M', 'G', 'T', 'E');
begin
  pre := 0;
  while Sz >= 1024 do
  begin
    inc(pre);
    Sz := Sz div 1024;
  end;
  Result := IntToStr(Sz) + ' ';
  if pre > 0 then
    Result := Result + cpre[pre];
  if postB then
    Result := Result + 'B';
end;

procedure RenewStaticDatas;
begin
  with Form1.ValueListEditor1 do
  begin
    Values[Keys[1]] := SizeDiv(allSent, True);
    Values[Keys[2]] := SizeDiv(allRecv, True);
    Values[Keys[3]] := IntToStr(allConn);
    if Integer(allPacked) > 0 then
      Values[Keys[4]] := SizeDiv(allPacked, True)
    else
      Values[Keys[4]] := '0 B';
    {
    if allWkgTime = 0 then
      exit;
    }
    if allWkgTime<1 then
      allWkgTime:=1;

    Values[Keys[5]] := SizeDiv(Round(allRecv * 60 / allWkgTime), True) + 'pm';
    Values[Keys[6]] := SizeDiv(Round(allSent * 60 / allWkgTime), True) + 'pm';
    Values[Keys[7]] := SizeDiv(Round(allConn * 60 / allWkgTime), True) + 'pm';
    Values[Keys[8]] := IntToStr(Round(allWkgTime / 60)) + ' min';
  end;
end;

var
  Chart1Count: Integer;
  PrevSent: DWORD = 0;
  PrevRecv: DWORD = 0;
  PrevConn: DWORD = 0;

procedure RenewCharts;
begin
  with Form1.Chart1 do
  begin
    if Chart1Count >= MaxPointsPerPage then
    begin
      Series[0].Delete(0);
      Series[1].Delete(0);
      Series[2].Delete(0);
    end;

    Series[0].AddXY(Time, (allSent - PrevSent)/1024, '');
    Series[1].AddXY(Time, (allRecv - PrevRecv)/1024, '');
    Series[2].AddXY(Time, allConn - PrevConn, '');

    PrevSent := allSent;
    PrevRecv := allRecv;
    PrevConn := allConn;
  end;
  inc(Chart1Count);

  with Form1.Chart2 do
  begin
    Series[0].YValue[0] := allSent + allRecv;
    if Integer(allPacked) > 0 then
      Series[0].YValue[1] := allPacked + 1
    else
      Series[0].YValue[1] := allPacked + 1;
  end;

  with Form1.Chart3 do
  begin
    Series[0].YValue[0] := allRecv + 1;
    Series[0].YValue[1] := allSent + 1;
  end;
end;

initialization
  allRecv := 0;
  allSent := 0;
  allConn := 0;
  allWkgTime := 0;
  allPacked := 0;
end.
